import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test_qq/main.dart';
import 'package:flutter_test_qq/mainPage.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:sqflite/sqflite.dart';
import 'package:path/path.dart';
import 'dataBase.dart';
class updatePwd extends StatefulWidget{

  const updatePwd({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState()=>updatePwdState();

}
var qqId = '';
var qqname = '';
var qqpwd = '';
get() async{
  SharedPreferences prefs = await SharedPreferences.getInstance();
  qqId = prefs.getString("QQ").toString();
  qqname =  prefs.getString("Name").toString();
  print('qqid${qqId}');
}
class updatePwdState extends State<updatePwd>{
  TextEditingController pwd = new TextEditingController();
  TextEditingController pwd2 = new TextEditingController();
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    get();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          backgroundColor:Colors.lightGreen,
          leading: IconButton(
            icon: Container(
              padding: EdgeInsets.all(1.0),
              child: Icon(Icons.keyboard_arrow_left),
            ),
            onPressed: (){
              Navigator.of(context).pop();
            },
          ),
          title: Text('密码修改',style:TextStyle(fontSize: 20,),),
        ),
        body: Column(
          children: [
            //  密码输入
            SizedBox(height: 10,),
            Padding(
                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                child: SizedBox(
                  child:  TextField(
                    controller: pwd,
                    obscureText: true,
                    // inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[A-Za-z0-9]')),
                    //   LengthLimitingTextInputFormatter(8),],
                    decoration: InputDecoration(
                      hintText: '密码',
                      filled: true,
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0x00BB0000)),
                        borderRadius: BorderRadius.all(
                          Radius.circular(15),
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0x00000000)),
                        borderRadius: BorderRadius.all(
                          Radius.circular(15),
                        ),
                      ),
                    ),
                  ),
                  width: 340,
                )
            ),
            //  确认密码
            SizedBox(height: 10,),
            Padding(
                padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
                child: SizedBox(
                  child: TextField(
                    controller: pwd2,
                    obscureText: true,
                    // inputFormatters: [FilteringTextInputFormatter.allow(RegExp('[A-Za-z0-9]{8，12}')),],
                    decoration: InputDecoration(
                      hintText: '确认密码',
                      filled: true,
                      enabledBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0x00BB0000)),
                        borderRadius: BorderRadius.all(
                          Radius.circular(15),
                        ),
                      ),
                      focusedBorder: OutlineInputBorder(
                        borderSide: BorderSide(color: Color(0x00000000)),
                        borderRadius: BorderRadius.all(
                          Radius.circular(15),
                        ),
                      ),
                    ),
                  ),
                  width: 340,
                )
            ),
            //  注册按钮
            SizedBox(height: 10,),
            MaterialButton(
              child: new Text('确    认',style: TextStyle(color: Colors.white),),
              color: Colors.lightGreen,
              onPressed: () {
                String regExp ="^(?![0-9]+\$)(?![a-zA-Z]+\$)[0-9A-Za-z]{8,24}\$";
                print(RegExp(regExp).hasMatch(pwd2.text));
                if(RegExp(regExp).hasMatch(pwd2.text) == true){
                  qqpwd = pwd2.text;
                  queryDatabase(context);
                  Fluttertoast.showToast(msg: '修改成功',textColor: Colors.white);
                  Navigator.of(context)
                      .push(MaterialPageRoute(builder: (BuildContext context) {
                    return mainPage();
                  }));
                }
                else if((RegExp(regExp).hasMatch(pwd2.text) == false && pwd2.text.length>24)
                ||(RegExp(regExp).hasMatch(pwd2.text) == false && pwd2.text.length<8)
                ){
                  Fluttertoast.showToast(msg: '请确保密码长度不小于8并不大于24',textColor: Colors.white);
                }
                else if(pwd2.text != pwd.text){
                  Fluttertoast.showToast(msg: '请确保两次密码输入一致',textColor: Colors.white);
                }
                else{
                  Fluttertoast.showToast(msg: '请确保密码同时且只含有字母和数字',textColor: Colors.white);
                }

              },
            ),
          ],
        ),
      ),
    );
  }
  queryDatabase(BuildContext context) async {
    TableTestProvider testProvider = TableTestProvider();
    //  通过getDatabasePath()方法获取数据库位置
    var databasePath = await getDatabasesPath();
    String path = join(databasePath, 'demo.db');
    // deleteDatabase(path);
    await testProvider.open(path);
    // 查询数据
    Test dd = await testProvider.getTodo(qqId);
    print('查询修改前${qqId}:${dd.toMap()}');
      // 新增注册数据
      Test test1 = Test();
      test1.id = qqId;
      test1.pwd = pwd.text;
      test1.name = qqname;
      await testProvider.update(test1);
      Fluttertoast.showToast(msg: '修改成功',textColor: Colors.green);
      Test td = await testProvider.getTodo(qqId);
      print('查询修改后${qqId}:${td.toMap()}');
      Navigator.of(context)
          .push(MaterialPageRoute(builder: (BuildContext context) {
        return Login();
      }));
    // 关闭数据库
    testProvider.close();
  }
}